//
//  ；
//  STPhotoBroeser
//
//  Created by StriEver on 16/3/16.
//  Copyright © 2016年 StriEver. All rights reserved.
//

#import "STPhotoBroswer.h"
//#import "STImageVIew.h"
#define MAIN_BOUNDS   [UIScreen mainScreen].bounds
#define Screen_Width  [UIScreen mainScreen].bounds.size.width
#define Screen_Height [UIScreen mainScreen].bounds.size.height
//图片距离左右 间距
#define SpaceWidth    10
@interface STPhotoBroswer ()<UIScrollViewDelegate>
@property (nonatomic, strong) NSArray * imageArray;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UILabel * numberLabel;
@property (nonatomic, strong) NSMutableArray *photoName;
@end
@implementation STPhotoBroswer
- (instancetype)initWithImageArray:(NSArray *)imageArray currentIndex:(NSInteger)index photoName:(NSMutableArray *)photoname{
    if (self == [super init]) {
        self.imageArray = imageArray;
        self.photoName = photoname;
        self.index = index;
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
        self.scrollView.contentOffset = CGPointMake(index * Screen_Width, 0);
        self.scrollView.backgroundColor = [UIColor blackColor];
        
        self.scrollView.bounces = YES;
        self.scrollView.contentSize =CGSizeMake(Screen_Width * self.imageArray.count, 0);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.delegate = self;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        
        
        [self.scrollView addGestureRecognizer:tap];
        
        _numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, Screen_Width, 40)];
                _numberLabel.textAlignment = NSTextAlignmentCenter;
                _numberLabel.textColor = [UIColor whiteColor];
                _numberLabel.text = [NSString stringWithFormat:@"%ld/%lu",self.index +1,(unsigned long)self.imageArray.count];
        
        for (int i = 0; i<self.imageArray.count; i++) {

            UIScrollView *smallscrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(Screen_Width * i, 150, Screen_Width, Screen_Height - 300)];
            smallscrollView.delegate = self;
            smallscrollView.minimumZoomScale = 0.5;
            smallscrollView.maximumZoomScale = 2;
            smallscrollView.pagingEnabled = YES;
            smallscrollView.showsHorizontalScrollIndicator = NO;
            smallscrollView.showsVerticalScrollIndicator = NO;
            [self.scrollView addSubview:smallscrollView];
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - 300)];
            imageView.userInteractionEnabled = YES;
            [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[i]]];
            [smallscrollView addSubview:imageView];
            //修改，有问题
//            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width  * i,  Screen_Height - 50, 200, 30)];
//            [label setFont:[UIFont systemFontOfSize:13]];
//            [label setTextColor:[UIColor whiteColor]];
//            label.text = self.photoName[i];
//            [self addSubview:label];
        }
        
         [self addSubview:_scrollView];
         [self addSubview:_numberLabel];
    }
    return self;
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    
    return scrollView.subviews[0];
}



- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    
    if (scrollView.zoomScale <= 1) {
        UIImageView *imageView = scrollView.subviews[0];
        imageView.center = CGPointMake(scrollView.frame.size.width/2, scrollView.frame.size.height/2);
    }
}



- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x / Screen_Width;
    _numberLabel.text = [NSString stringWithFormat:@"%ld/%lu",index +1,(unsigned long)self.imageArray.count];
      UIScrollView *bigScrollView = self.subviews[0];
    if (![bigScrollView isEqual:scrollView]) {
        return;
    }
    
}



- (void)show{
     UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.frame = CGRectMake(0, 0, Screen_Width, Screen_Height);
    [window addSubview:self];
    self.transform = CGAffineTransformMakeScale(0, 0);
    [UIView animateWithDuration:.5 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];
}
- (void)dismiss{
     self.transform = CGAffineTransformIdentity;
    [UIView animateWithDuration:.5 animations:^{
        self.transform = CGAffineTransformMakeScale(0.0000000001, 0.00000001);
    }completion:^(BOOL finished) {
         [self removeFromSuperview];
    }];
   
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    [self dismiss];
}
@end
